<?php

namespace app\web\controller;

use app\web\controller\Base;
use think\Db;
use QRcode;

class Card extends Base {

    public function index() {

        $_GPC = input();
        $title = '我的卡券';
       $_GPC['page']= isset($_GPC['page'])?$_GPC['page']:1;
        
        $_GPC['limit']= isset($_GPC['limit'])?$_GPC['limit']:1;
         
        $page = max(intval($_GPC['page']), 1);
        $limit = max(intval($_GPC['limit']), 10);

        $row = Db::name('sale_card')->where('sale_id', $_GPC['sale_id'])->where('member_id', session('fans')['id']);
        $list = $row->page($page, $limit)->order('status', 'ASC')->order('id', 'DESC')->select();
        $count = $row->count();
        $sale = pdo_get('sale', array('id' => $_GPC['sale_id']));
        $sale['card'] = json_decode($sale['card'], true);

        $this->view->assign('page', $page);
        $this->view->assign('limit', $limit);
        
         $this->view->assign('card', $sale['card'] );
        
        $this->view->assign('count', $count);
        $this->view->assign('list', $list);
        $this->view->assign('sale', $sale);
        $this->view->assign('title', $title);
        return $this->view->fetch();
    }

    public function create() {

        $_GPC = input();
        $sale = pdo_get('sale', array('id' => $_GPC['sale_id']));
        $sale['replace'] = json_decode($sale['replace'], true);
        $card = json_decode($sale['card'], true);
        if (!isset($card['list'])) {
            $card['idx'] = 0;
            $card['list'][] = array('name' => $card['name'], 'term' => $card['term'], 'starttime' => $card['starttime'],
                'endtime' => $card['endtime'], 'num' => $card['num'], 'limit' => $card['limit'], 'desc' => $card['desc']);
        }
        $member_sale = pdo_get('member_sale', array('sale_id' => $_GPC['sale_id'], 'member_id' => session('fans')['id']));
        if (!$member_sale) {
            $result = pdo_insert('member_sale', array('member_id' => session('fans')['id'], 'member' => json_encode(session('fans')), 'sale_id' => $_GPC['sale_id'], 'createtime' => time()));
            if (!empty($result) || $result === 0) {
                
            } else {
                json('error', 1, '提交失败！');
            }
        }
         $_W['baseurl']=$this->request->domain(true);
        if ($sale['paytype'] == 0) {
            $cardnum = 0;
            foreach ($card['list'] as $key => $value) {

                if ($value['limit'] > 0) {
                    $sale_card_count = Db::name('sale_card')->where(array('sale_id' => $_GPC['sale_id'], 'member_id' => session('fans')['id'], 'cardidx' => $key))->count();
                    if ($value['limit'] <= $sale_card_count) {
                        continue;
                    }
                }
                if ($value['num'] > 0) {
                    $sale_card_counts = Db::name('sale_card')->where(array('sale_id' => $_GPC['sale_id'], 'cardidx' => $key))->count();
                    if ($value['num'] < $sale_card_counts) {
                        continue;
                    }
                }
                $result = pdo_insert('sale_card', array('member_id' => session('fans')['id'], 'sale_id' => $_GPC['sale_id'], 'card' => json_encode($value), 'cardidx' => $key, 'code' => create_order_sn(), 'status' => '0', 'createtime' => time(), 'updatetime' => time()));
                if (!empty($result) || $result === 0) {
                    $cardnum++;
                }
            }
           
            if ($cardnum > 0) {
                $url = $_W['baseurl'] . '/web/video?mid=' .
                        session('fans')['id'] .
                        '&sale_id=' . $_GPC['sale_id'] .
                        '&access_token=' . session('fans')['access_token'] .
                        '&idx=' . $_GPC['idx'] .
                        '&time=' . time();
                pdo_insert('sale_video', array('member_id' => session('fans')['id'], 'sale_id' => $_GPC['sale_id'], 'url' => $url));
                asyn_http_get($url);
                if ($sale['jump'] == 'card') {
                    $jump_url = $_W['baseurl'] . '/web/card?sale_id=' . $sale['id'];
                } elseif ($sale['jump'] == 'real_share') {
                    // $url = get_redirect_url($sale['real_share']);
                    // $urlarr = parse_url($url);
                    // $urlpath = explode('/', $urlarr['path']);
                    //if (isset($urlpath[3])) {
                        if (is_iphone()) {
                            $jump_url = 'snssdk1128://user/profile/' .$sale['real_share'] . '?refer=web&gd_label=click_wap_profile_bottom&type=need_follow&needlaunchlog=1';
                        } else {
                            $jump_url = 'snssdk1128://user/profile/' .$sale['real_share'] . '?refer=web&gd_label=click_wap_download_follow&type=need_follow&needlaunchlog=1';
                        }
                    // } else {
                    //     $jump_url = isset($sale['real_share']) ? $sale['real_share'] : $_W['baseurl'] . '/web/card?sale_id=' . $sale['id'];
                    // }
                } else {
                    $jump_url = isset($sale['jump_url']) ? $sale['jump_url'] : $_W['baseurl'] . '/web/card?sale_id=' . $sale['id'];
                }
                json($jump_url, 0, '领取成功！');
            } else {
                json('error', 1, $sale['replace']['cart_error'] ? $sale['replace']['cart_error'] : '领取失败！');
            }
        } else {
            $url = $_W['baseurl'] . '/web/video?mid=' .
                    session('fans')['id'] .
                    '&sale_id=' . $_GPC['sale_id'] .
                    '&access_token=' . session('fans')['access_token'] .
                    '&idx=' . $_GPC['idx'] .
                    '&time=' . time();
            pdo_insert('sale_video', array('member_id' => session('fans')['id'], 'sale_id' => $_GPC['sale_id'], 'url' => $url));
            asyn_http_get($url);
            if ($sale['jump'] == 'card') {
                $jump_url = $_W['baseurl'] . '/web/card?sale_id=' . $sale['id'];
            } elseif ($sale['jump'] == 'real_share') {
                // $url = get_redirect_url($sale['real_share']);
                // $urlarr = parse_url($url);
                // $urlpath = explode('/', $urlarr['path']);
                //if (isset($urlpath[3])) {
                    if (is_iphone()) {
                        $jump_url = 'snssdk1128://user/profile/' . $sale['real_share']. '?refer=web&gd_label=click_wap_profile_bottom&type=need_follow&needlaunchlog=1';
                    } else {
                        $jump_url = 'snssdk1128://user/profile/' .$sale['real_share'] . '?refer=web&gd_label=click_wap_download_follow&type=need_follow&needlaunchlog=1';
                    }
                // } else {
                //     $jump_url = isset($sale['real_share']) ? $sale['real_share'] : $_W['baseurl'] . '/web/card?sale_id=' . $sale['id'];
                // }
            } else {
                $jump_url = isset($sale['jump_url']) ? $sale['jump_url'] : $_W['baseurl'] . '/web/card?sale_id=' . $sale['id'];
            }
            json($jump_url, 0, '操作成功！');
        }
        json('error', 1, '提交失败！');
    }

    public function qrcode() {
        $_GPC = input();
        header('content-type: image/png');
        require_once '../extend/qrcode/phpqrcode.php';
        ob_start();
         $_W['baseurl']=$this->request->domain(true);
        QRcode::png($_W['baseurl'] . '/web/card/verifyer?id=' . $_GPC['id'], false, 'L', 5, 2);
        $qrcode = ob_get_contents();
        ob_end_clean();
        exit($qrcode);
    }

    public function verifyer() {

        $_GPC = input();
        $sale_card = pdo_get('sale_card', array('id' => $_GPC['id']));
        $sale_verifyer = pdo_get('sale_verifyer', array('sale_id' => $sale_card['sale_id'], 'member_id' => session('fans')['id']));
        $status = 0;
        if ($sale_verifyer) {
            $card = json_decode($sale_card['card'], true);
            $sale = pdo_get('sale', array('id' => $sale_card['sale_id']));
            $endtime = time();
            if ($card['endtime']) {
                $endtime = strtotime($card['endtime']) + 86399;
            } else {
                $endtime = $sale_card['createtime'] + $card['term'] * 86400;
            }
            if (!$card['starttime'] || $card['starttime'] && strtotime($card['starttime']) < time()) {
                if ($card['term'] == 0 || $endtime > time()) {
                    pdo_update('sale_card', array('status' => 1), array('id' => $sale_card['id']));
                    $status = 1;
                    $sale_card['status'] = 1;
                }
            }
        }
        $this->view->assign('_GPC', $_GPC);
        $this->view->assign('status', $status);
        $this->view->assign('sale', $sale);
        $this->view->assign('sale_card', $sale_card);
        $this->view->assign('sale_verifyer', $sale_verifyer);
        $this->view->assign('card', $card);
        return $this->view->fetch();
    }

    public function pwdverifyer() {

        $_GPC = input();
        $sale_card = pdo_get('sale_card', array('id' => $_GPC['id']));
        $sale = pdo_get('sale', array('id' => $sale_card['sale_id']));
        $card = json_decode($sale['card'], true);
        if ($card['pwd'] && $card['pwd'] == $_GPC['pwd']) {
            pdo_update('sale_card', array('status' => 1), array('id' => $sale_card['id']));
            json('success', 0, '核销成功');
        } else {
            json('error', 1, '核销失败');
        }
    }

}
